package com.kye.express.business.order.scan_code_addresses;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemScanCodeAddressBinding;
import com.kye.express.view.reclyerview.OnItemClickListener;
import com.kye.model.bean.order.scan_code_order.ScanCodeOrderAddress;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;
import me.drakeet.multitype.ItemViewBinder;

public class AddressDataViewBinder extends ItemViewBinder<ScanCodeOrderAddress, AddressDataViewBinder.ViewHolder> {

    private OnItemClickListener<ScanCodeOrderAddress> mOnItemClickListener;
    private OnItemClickListener<ScanCodeOrderAddress> mOnEditClickListener;
    private OnItemClickListener<ScanCodeOrderAddress> mOnDeleteClickListener;

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_scan_code_address, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull ScanCodeOrderAddress addressData) {
        holder.bind(addressData);
    }

    @Override
    protected long getItemId(@NonNull ScanCodeOrderAddress item) {
        return item.getGuid().hashCode();
    }

    public OnItemClickListener<ScanCodeOrderAddress> getOnItemClickListener() {
        return mOnItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener<ScanCodeOrderAddress> onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    public OnItemClickListener<ScanCodeOrderAddress> getOnEditClickListener() {
        return mOnEditClickListener;
    }

    public void setOnEditClickListener(OnItemClickListener<ScanCodeOrderAddress> onEditClickListener) {
        mOnEditClickListener = onEditClickListener;
    }

    public OnItemClickListener<ScanCodeOrderAddress> getOnDeleteClickListener() {
        return mOnDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnItemClickListener<ScanCodeOrderAddress> onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private final ItemScanCodeAddressBinding mBind;
        private ScanCodeOrderAddress mAddressData;

        ViewHolder(View itemView) {
            super(itemView);
            mBind = ItemScanCodeAddressBinding.bind(itemView);

            RxView.clicks(mBind.linearLayoutEdit)
                    .filter(new Predicate<Object>() {
                        @Override
                        public boolean test(Object o) throws Exception {
                            return mOnEditClickListener != null;
                        }
                    })
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            mOnEditClickListener.onClickItem(mAddressData, mBind.linearLayoutEdit);
                        }
                    });

            RxView.clicks(mBind.linearLayoutDelete)
                    .filter(new Predicate<Object>() {
                        @Override
                        public boolean test(Object o) throws Exception {
                            return mOnDeleteClickListener != null;
                        }
                    })
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            mOnDeleteClickListener.onClickItem(mAddressData, mBind.linearLayoutDelete);
                        }
                    });

            RxView.clicks(mBind.getRoot())
                    .filter(new Predicate<Object>() {
                        @Override
                        public boolean test(Object o) throws Exception {
                            return mOnItemClickListener != null;
                        }
                    })
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {
                        @Override
                        public void accept(Object o) throws Exception {
                            mOnItemClickListener.onClickItem(mAddressData, mBind.linearLayoutDelete);
                        }
                    });
        }

        public void bind(ScanCodeOrderAddress addressData) {
            mAddressData = addressData;
            mBind.setAddress(addressData);
        }
    }
}
